package com.fingard.dsp.bank.directbank.chinapay05;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import org.apache.commons.httpclient.NameValuePair;

/**
 * 代付备付金余额查询
 */
public class ChinaPay05Balance extends ChinaPay05Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        retAts.writeStart();

        try {
            while (p_req7506.readRecord()) {
                ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "bal");
                if (actItem == null) {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "未找到账号" + p_req7506.readedDetail.actNum + "对应的商户配置信息";
                    retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                    retAts.writeEnd();
                    return retAts;
                }
                String merchantId = actItem.merchantId;
                String msg = merchantId + "20090501";// 商户号 + 版本号
                WriteBankLogLn("待签名的数据:" + msg);
                String chkValue = sign(msg, merchantId, actItem.ownKeyStorePath, "bal");
                WriteBankLogLn2("签名后的chkValue:" + chkValue);
                //填入各个表单域的值
                NameValuePair[] data = {
                        new NameValuePair("merId", merchantId),
                        new NameValuePair("version", "20090501"),
                        new NameValuePair("signFlag", "1"),
                        new NameValuePair("chkValue", chkValue),
                };
                String reqStr = "merId=" + merchantId + "&version=20090501&signFlag=1&chkValue=" + chkValue;
                WriteBankLogStep2(reqStr);
                String url = bankFront.getUrl("provisionsBal");

                String result = sendBatch(data, url, "bal");
                if (StringHelper.hasAnyChar(result)) {
                    if (result.indexOf('|') > 0) {
                        String[] response = result.split("\\|");
                        String tmpCode = response[0];
                        if ("000".equals(tmpCode)) {
                            String merAmt = response[2];
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = "查询成功";
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                            tmpRetDetail.currBal = StringHelper.amountDivide100(merAmt);
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = "查询出错，具体原因需咨询银行";
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "银行返回报文格式非法!";
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行返回报文为空";
                }
            }
            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
